package com.shbaiche.bifenba.ui.match;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sevenheaven.segmentcontrol.SegmentControl;
import com.shbaiche.bifenba.App;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.adapter.match.MatchFightHistoryListAdapter;
import com.shbaiche.bifenba.adapter.match.MatchListAdapter;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.data.CompeteVo;
import com.shbaiche.bifenba.model.match.GuessType;
import com.shbaiche.bifenba.model.match.MatchesAnalyseMessage;
import com.shbaiche.bifenba.model.match.MatchesGuessMessage;
import com.shbaiche.bifenba.model.match.MatchesListMessage;
import com.shbaiche.bifenba.model.match.MatchesOutsMessage;
import com.shbaiche.bifenba.model.match.MatchesVo;
import com.shbaiche.bifenba.model.match.TeamHistoryVo;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.base.BaseFragment;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroid.base.SimpleRecAdapter;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;
import okhttp3.Call;

/**
 * 比赛详情-赔率
 */

@SuppressLint("ValidFragment")
public class MatchDetailAnalysisFragment extends BaseFragment {
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout refreshLayout;

    @BindView(R.id.layout_value_win)
    LinearLayout layoutValueWin;

    @BindView(R.id.layout_value_flat)
    LinearLayout layoutValueFlat;

    @BindView(R.id.layout_value_lose)
    LinearLayout layoutValueLose;

    @BindView(R.id.layout_support_win)
    LinearLayout layoutSupportWin;

    @BindView(R.id.layout_support_flat)
    LinearLayout layoutSupportFlat;

    @BindView(R.id.layout_support_lose)
    LinearLayout layoutSupportLose;

    @BindView(R.id.tv_value_win)
    TextView tvValueWin;

    @BindView(R.id.tv_value_flat)
    TextView tvValueFlat;

    @BindView(R.id.tv_value_lost)
    TextView tvValueLost;

    @BindView(R.id.tv_name_home)
    TextView tvNameHome;

    @BindView(R.id.tv_name_visit)
    TextView tvNameVisit;

    @BindView(R.id.tv_desc)
    TextView tvDesc;

    @BindView(R.id.layout_fight)
    LinearLayout layoutFight;

    @BindView(R.id.tv_fight_home)
    TextView tvFightHome;

    @BindView(R.id.tv_fight_flat)
    TextView tvFightFlat;

    @BindView(R.id.tv_fight_visit)
    TextView tvFightVisit;

    @BindView(R.id.contentLayout)
    XRecyclerContentLayout contentLayout;

    MatchFightHistoryListAdapter adapter;

    MatchesVo mMatchVo;

    private Dialog dg;

    @SuppressLint("ValidFragment")
    public MatchDetailAnalysisFragment(){

    }

    public static MatchDetailAnalysisFragment newInstance() {
        return new MatchDetailAnalysisFragment();
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        if(savedInstanceState!=null && savedInstanceState.containsKey("data")){
            mMatchVo = (MatchesVo)savedInstanceState.getSerializable("data");
        }else{
            mMatchVo = (MatchesVo)getArguments().getSerializable("data");
        }

        if(mMatchVo!=null){
            tvNameHome.setText(mMatchVo.getHomeName());
            tvNameVisit.setText(mMatchVo.getVisitName());

            dg = new Dialog(getActivity());
            dg.setContentView(R.layout.view_loading);

            initAdapter();
            loadData();

            refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    loadData();
                }
            });
        }

    }

    @OnClick({
            R.id.layout_support_win,
            R.id.layout_support_flat,
            R.id.layout_support_lose,

    })
    public void clickEvent(View view) {
        switch (view.getId()) {
            case R.id.layout_support_win:
                matchGuess(GuessType.HOMEWIN);
                break;
            case R.id.layout_support_flat:
                matchGuess(GuessType.DRAW);
                break;
            case R.id.layout_support_lose:
                matchGuess(GuessType.VISITWIN);
                break;
        }
    }



    private void initAdapter() {
        setLayoutManager(contentLayout.getRecyclerView());
        contentLayout.getRecyclerView()
                .setAdapter(getAdapter());
        contentLayout.getRecyclerView()
                .setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener() {
                    @Override
                    public void onRefresh() {
                        loadData();
                    }

                    @Override
                    public void onLoadMore(int page) {
                        loadData();
                    }
                });

        contentLayout.loadingView(View.inflate(getContext(), R.layout.view_loading, null));
        contentLayout.getRecyclerView().useDefLoadMoreView();
    }

    private void setData(MatchesAnalyseMessage message){
        if(message!=null){
            LinearLayout.LayoutParams lpSupportWin,lpSupportFlat,lpSupportLose;
            lpSupportWin  = new LinearLayout.LayoutParams(-1,0,80+message.getHomeSupport());
            lpSupportFlat = new LinearLayout.LayoutParams(-1,0,80+message.getDrawSupport());
            lpSupportLose = new LinearLayout.LayoutParams(-1,0,80+message.getVisitSupport());

            layoutValueWin.setLayoutParams(lpSupportWin);
            layoutValueFlat.setLayoutParams(lpSupportFlat);
            layoutValueLose.setLayoutParams(lpSupportLose);


            tvValueWin.setText(message.getHomeSupport()+"%");
            tvValueFlat.setText(message.getDrawSupport()+"%");
            tvValueLost.setText(message.getVisitSupport()+"%");

            tvDesc.setText("最近"+message.getFightNum()+"场交锋");

            layoutFight.setWeightSum(message.getFightNum());

            LinearLayout.LayoutParams lpFightHome,lpFightFlat,lpFightVisit;
            lpFightHome  = new LinearLayout.LayoutParams(0,-1,message.getHomeWin());
            lpFightFlat  = new LinearLayout.LayoutParams(0,-1,message.getDraw());
            lpFightVisit  = new LinearLayout.LayoutParams(0,-1,message.getVisitWin());

            tvFightHome.setLayoutParams(lpFightHome);
            tvFightFlat.setLayoutParams(lpFightFlat);
            tvFightVisit.setLayoutParams(lpFightVisit);

            tvFightHome.setText(message.getHomeWin()+"胜");
            tvFightFlat.setText(message.getDraw()+"平");
            tvFightVisit.setText(message.getVisitWin()+"胜");

            adapter.setData(message.getVoList());

        }
    }

    public void loadData() {
        MatchesAnalyseMessage message = new MatchesAnalyseMessage();
        message.setCompeteId(mMatchVo.getCompeteId());
        message.setGameType(mMatchVo.getGameType());
        message.setGameId(mMatchVo.getGameId());

        NetApi.matchesAnalyse(message, new JsonCallback<TransMessage>() {
            @Override
            public void onFail(Call call, Exception e, int id) {
                refreshLayout.setRefreshing(false);
                ToastUtil.show("onFail" + e.getMessage());
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                refreshLayout.setRefreshing(false);
                ToastUtil.show("onFail" + e.getMessage());

            }

            @Override
            public void onResponse(TransMessage response, int id) {
                refreshLayout.setRefreshing(false);
                MatchesAnalyseMessage result =new Gson().fromJson(response.getMessage(),MatchesAnalyseMessage.class);
                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, result.getCode())) {
                    if(result!=null){
                        Log.e("t","onResponse");
                        setData(result);

                    }
                }else{
                    Log.e("t","onResponse getCode "+response.getCode());
                    ToastUtil.show(result.getMessage());
                }
            }
        });

    }

    public void matchGuess(GuessType guessType) {
        if (App.getPassport() == null) {
            ToastUtil.show("请先登录!");
        }else{
            dg.show();
            MatchesGuessMessage message = new MatchesGuessMessage();
            message.setCompeteId(mMatchVo.getCompeteId());
            message.setGameType(mMatchVo.getGameType());
            message.setGameId(mMatchVo.getGameId());
            message.setPassport(App.getPassport());
            message.setGuessType(guessType);

            NetApi.matchesGuess(message, new JsonCallback<TransMessage>() {
                @Override
                public void onFail(Call call, Exception e, int id) {
                    refreshLayout.setRefreshing(false);
                    ToastUtil.show("onFail" + e.getMessage());
                    dg.dismiss();
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    refreshLayout.setRefreshing(false);
                    ToastUtil.show("onFail" + e.getMessage());
                    dg.dismiss();
                }

                @Override
                public void onResponse(TransMessage response, int id) {
                    refreshLayout.setRefreshing(false);
                    dg.dismiss();
                    MatchesGuessMessage result =new Gson().fromJson(response.getMessage(),MatchesGuessMessage.class);
                    if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, result.getCode())) {
                        if(result!=null){
                            Log.e("t","onResponse");
                            ToastUtil.show(result.getMessage());
                            loadData();
                        }
                    }else{
                        Log.e("t","onResponse getCode "+response.getCode());
                        ToastUtil.show(result.getMessage());
                    }
                }
            });
        }


    }

    public SimpleRecAdapter getAdapter() {
        if (adapter == null) {
            adapter = new MatchFightHistoryListAdapter(getActivity(),mMatchVo);
            adapter.setRecItemClick(new RecyclerItemCallback<TeamHistoryVo, MatchFightHistoryListAdapter.ViewHolder>() {
                @Override
                public void onItemClick(int position, TeamHistoryVo model, int tag, MatchFightHistoryListAdapter.ViewHolder holder) {
                    super.onItemClick(position, model, tag, holder);
                }
            });
        }
        return adapter;
    }


    public void setLayoutManager(XRecyclerView recyclerView) {
        recyclerView.verticalLayoutManager(context);
    }






    @Override
    public int getLayoutId() {
        return R.layout.fragment_match_detail_analysis;
    }
}
